package cn.rkylin.oms.system.log.controller;

import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;
import cn.rkylin.oms.common.dto.PagedData;
import cn.rkylin.oms.system.log.domain.LogRecord;
import cn.rkylin.oms.system.log.domain.SearchConditions;
import cn.rkylin.oms.system.log.service.ILogService;
import cn.rkylin.oms.system.project.dao.ProjectManageDAOImpl;
import cn.rkylin.oms.system.project.vo.ProjectVO;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;

import static cn.rkylin.oms.common.MediaType.APPLICATION_JSON_VALUE_UTF8;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/log")
public class LogController {
	
    protected static final String JSON_RESULT = "result";
    protected static final String SUCCESS = "success";
    protected static final String RETURN_DATA = "data";
    
	@Autowired
	private ILogService logService;

	@RequestMapping(value = "/search", method = RequestMethod.GET, produces = APPLICATION_JSON_VALUE_UTF8)
	/*public PagedData<LogRecord> search(SearchConditions conditions,
									   @PageableDefault Pageable pageable) throws Exception {
		conditions.decode();
		return logService.search(pageable, conditions);
	}*/
	public Map search(String quickSearch,String keyword,String bizId, String tag, String user,String operation,String operTimeStart,String operTimeEnd,
			@RequestParam(required = false, defaultValue = "0") int start,
            @RequestParam(required = false, defaultValue = "10") int length) throws Exception {
		Map map = new HashMap();
        try {
            quickSearch = URLDecoder.decode(quickSearch, "UTF-8");
            if(!StringUtils.isEmpty(keyword)) {
            	keyword = URLDecoder.decode(keyword, "UTF-8");
            }
            if(!StringUtils.isEmpty(bizId)) {
            	bizId = URLDecoder.decode(bizId, "UTF-8");
            }
            if(!StringUtils.isEmpty(tag)) {
            	tag = URLDecoder.decode(tag, "UTF-8");
            }
            if(!StringUtils.isEmpty(user)) {
            	user = URLDecoder.decode(user, "UTF-8");
            }
            if(!StringUtils.isEmpty(operation)) {
            	operation = URLDecoder.decode(operation, "UTF-8");
            }
            // 处理分页
            if (length == -1) {
                length = Integer.MAX_VALUE;
            }
            int page = start / length + 1;
            LogRecord logRecord=new LogRecord(); 
            logRecord.setBizId(bizId);
            logRecord.setTag(tag);
            logRecord.setUser(user);
            logRecord.setOperation(operation);
            logRecord.setDetail(keyword);
            PageInfo<LogRecord> logRecordList = logService.findByWhere(page, length, logRecord);
            map.put(JSON_RESULT, SUCCESS);
            map.put("recordsFiltered", logRecordList.getTotal());
            map.put("recordsTotal", logRecordList.getTotal());
            map.put(RETURN_DATA, logRecordList.getList());

        } catch (Exception ex) {
            ex.printStackTrace();
            map.put("result", "failed");
            map.put("msg", ex.getMessage());
        }
		return map;
	}

}
